package com.zoe.onelink.core.autoconfigure;

import com.zoe.onelink.core.i18n.I18nMessageProvider;
import com.zoe.onelink.core.i18n.OnelinkResourceBundleMessageSource;
import com.zoe.onelink.core.util.LocaleResolverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.Locale;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-03-31
 */
public class OnelinkI18nAutoConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(OnelinkI18nAutoConfiguration.class);
    /**
     * 业务模块国际化翻译文件使用该前缀
     */
    public static final String BASE_MESSAGES = "i18n.onelink.messages";
    /**
     * core包国际化翻译文件前缀
     */
    public static final String BASE_CORE_MESSAGES = "i18n.onelink.core";
    /**
     * 授权服务国际化翻译文件使用该前缀
     */
    public static final String BASE_AUTH = "i18n.onelink.auth";

    /** 国际化cookie名称*/
    public static final String LOCALE_COOKIE_NAME = "cookieLocale";
    /** 国际化切换请求参数*/
    public static final String LOCALE_REQUEST_PARAM = "lang";

    public OnelinkI18nAutoConfiguration() {
        Locale.setDefault(Locale.CHINESE);
    }

    @Bean
    @Primary
    public MessageSource onelinkResourceBundleMessageSource(ObjectProvider<I18nMessageProvider> i18nMessageProvider) {
        OnelinkResourceBundleMessageSource messageSource = new OnelinkResourceBundleMessageSource(i18nMessageProvider.getIfAvailable());
        messageSource.setDefaultEncoding("utf-8");
        messageSource.addBasenames(BASE_MESSAGES);
        messageSource.addBasenames(BASE_CORE_MESSAGES);
        messageSource.addBasenames(BASE_AUTH);
        LOGGER.info("[OneLinkFrm] 开启[I18nAutoConfiguration]国际默认配置:{}", String.join(",", messageSource.getBasenameSet()));
        return messageSource;
    }

    @Bean
    public LocaleResolverUtil localeResolverUtil() {
        return new LocaleResolverUtil();
    }

    /**
     * 基于cookie的语言解析器
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(LocaleResolver.class)
    public LocaleResolver localeResolver() {
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
        cookieLocaleResolver.setCookieName(LOCALE_COOKIE_NAME);
        cookieLocaleResolver.setDefaultLocale(Locale.CHINESE);
        return cookieLocaleResolver;
    }

    /**
     * 默认拦截器 其中lang表示切换语言的参数名
     */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                LOGGER.info("[OneLinkFrm] 开启[LocaleChangeInterceptor]--请求?lang=zh/en切换国际化");
                LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
                localeInterceptor.setParamName(LOCALE_REQUEST_PARAM);
                registry.addInterceptor(localeInterceptor);
            }

        };
    }

}
